package org.example.leetcpde;

import org.junit.jupiter.api.Test;

import java.util.stream.IntStream;

public class minPathSum {

    @Test
    void aa() {
//        int[][] grid = {{1,2,3}, {4,5,6}};
        int[][] grid = {{1,3,1}, {1,5,1}, {4,2,1}};
        minPathSum(grid);
    }

    public int minPathSum(int[][] grid) {
        int len1 = grid.length, len2 = grid[0].length;
        int[] dp = new int[len2];
        dp[0] = grid[0][0];
        IntStream.range(1, len2).forEach(i -> dp[i] = dp[i - 1] + grid[0][i]);
        for (int i = 1; i < len1; i++) {
            dp[0] = dp[0] + grid[i][0];
            for (int j = 1; j < len2; j++) {
                dp[j] = Math.min(dp[j], dp[j - 1]) + grid[i][j];
            }
        }

        return dp[len2 - 1];
    }
}
